﻿using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CustomerContactSystem.Core.Infrastructure.Utility;
using CustomerContactSystem.Core.Model;

namespace CustomerContactSystem.WebForms.Enquiries.Customers {
    public partial class Enquiries : BasePage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsValidPageRequest(true, true, "customerid")) {
                return;
            }

            EnquiryHeader.Visible = false;
            if (Request.QueryString["id"] != null) {
                EnquiryHeader.Visible = true;
                EnquiryIdCaption.Text = Request.QueryString["id"];
                EnquiryIdCaption.NavigateUrl = "~/Enquiries/View.aspx?id=" + Request.QueryString["id"];
            }

            if (Request.QueryString["view"] != null) {
                GetEnquiries();
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=CustomerEnquiries.xls");
                Response.Charset = "";
                Response.ContentType = "application/vnd.ms-xls";
                var stringWriter = new StringWriter();
                var htmlWriter = new HtmlTextWriter(stringWriter);
                EnquiriesList.RenderControl(htmlWriter);
                Response.Write(stringWriter.ToString());
                Response.End();
            } else {
                ExcelLink.NavigateUrl = "~/Enquiries/Customers/Enquiries.aspx?customerid=" + Request.QueryString["customerid"] + "&view=excel";
                GetEnquiries();
            }
        }

        private void GetEnquiries() {
            var customerId = int.Parse(Request.QueryString["customerid"]);
            var enquiries = EnquiryRepository.QueryOver().Where(e => e.Customer.Id == customerId).List();

            EnquiriesList.DataSource = enquiries;
            EnquiriesList.DataBind();
        }

        protected void EnquiriesListItemDataBound(object sender, RepeaterItemEventArgs e) {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) {
                return;
            }

            var enquiry = (Enquiry)e.Item.DataItem;
            var enquiryRow = (HtmlTableRow)e.Item.FindControl("EnquiryRow");
            var ccIdCell = (HtmlTableCell)e.Item.FindControl("CcIdCell");
            var enquiryCell = (HtmlTableCell)e.Item.FindControl("EnquiryCell");
            var sourceCell = (HtmlTableCell)e.Item.FindControl("SourceCell");
            var receivedCell = (HtmlTableCell)e.Item.FindControl("ReceivedCell");
            var deadlineCell = (HtmlTableCell)e.Item.FindControl("DeadlineCell");
            var deptCell = (HtmlTableCell)e.Item.FindControl("DepartmentCell");
            var catCell = (HtmlTableCell)e.Item.FindControl("CategoryCell");
            var statusCell = (HtmlTableCell)e.Item.FindControl("StatusCell");
            var withCell = (HtmlTableCell)e.Item.FindControl("WithCell");
            var enquiryLink = (HyperLink)e.Item.FindControl("EnquiryLink");
            var enquiryDetail = (Literal)e.Item.FindControl("EnquiryDetail");

            var enquiryLinkText = "Anon:";
            if (enquiry.Customer != null) {
                enquiryLinkText = enquiry.Customer.FullName + ", " + enquiry.Customer.AddressLine + ":";
            }
            enquiryLinkText += enquiry.EnquiryDetail;
            enquiryLinkText = enquiryLinkText.HtmlStripped();

            if (Request.QueryString["view"] != null) {
                ccIdCell.Attributes.Add("style", "vertical-align:top;border: 1px solid #000;");
                enquiryCell.Attributes.Add("style", "vertical-align:top;border: 1px solid #000;");
                enquiryDetail.Text = enquiryLinkText;
                enquiryLink.Visible = false;
                sourceCell.Attributes.Add("style", "vertical-align:top;border: 1px solid #000;");
                receivedCell.Attributes.Add("style", "vertical-align:top;border: 1px solid #000;");
                deadlineCell.Attributes.Add("style", "vertical-align:top;border: 1px solid #000;");
                deptCell.Attributes.Add("style", "vertical-align:top;border: 1px solid #000;");
                catCell.Attributes.Add("style", "vertical-align:top;border: 1px solid #000;");
                statusCell.Attributes.Add("style", "vertical-align:top;border: 1px solid #000;");
                withCell.Attributes.Add("style", "vertical-align:top;border: 1px solid #000;");
                if (enquiry.IsConfidential) {
                    enquiryDetail.Text = "Confidential enquiry. Access only to those involved or admin users!";
                }
            } else {
                if (enquiry.Deadline < DateTime.Now && enquiry.Closure == null) {
                    enquiryRow.Attributes.Add("class", "overdue");
                    enquiryRow.Attributes.Add("title", enquiry.Rep != null ? "Made by respresentative - Overdue!" : "Overdue!");
                } else {
                    if (enquiry.Rep != null) {
                        enquiryRow.Attributes.Add("title", "Made by respresentative");
                    }
                }

                if (enquiryLinkText.Length > 30) {
                    enquiryLinkText = enquiryLinkText.Substring(0, 29) + "...";
                }
                enquiryLink.Text = enquiryLinkText;
                enquiryLink.NavigateUrl = "~/Enquiries/View.aspx?id=" + enquiry.Id;

                if (enquiry.IsConfidential) {
                    enquiryRow.Attributes.Add("class", "confidential");
                    enquiryRow.Attributes.Add("title", "Confidential enquiry. Access only to those involved or admin users!");
                    enquiryLink.Text = "Confidential";
                }
            }
        }
    }
}